import configer from '../types/configer';
import Phaser from 'phaser'

/** 资源加载场景 */
export default class Preloader extends Phaser.Scene {
    constructor() {
        super('Preloader');
    }


    init() {
        /** 获取场景大小 */
        let gameWidth = <number>this.sys.game.config.width;
        let gameHeight = <number>this.sys.game.config.height;
        // 背景图片
        let img = this.add.image(gameWidth / 2, gameHeight / 2, 'background');
        img.setSize(gameWidth, gameHeight);
        // 绘制进度条
        const barBox = this.add.rectangle(
            gameWidth / 2,
            gameHeight - 36,
            gameWidth,
            36,
            0x222056
        );
        barBox.setAlpha(0.5)
        const bar = this.add.rectangle(
            0,
            gameHeight - 36,
            6,
            28,
            0xffffff
        );
        bar.setAlpha(0.8)

        // let loginText = this.add.text(gameWidth / 2, gameHeight - 36, '点击登录', {
        //     fontSize: '14px',
        //     color: '#ffffff'
        // }).setOrigin(0.5);

        let loadText = this.add.text(gameWidth / 2, gameHeight - 36, 'Loading...', {
            fontSize: '14px',
            color: '#ffffff'
        }).setOrigin(0.5);
        this.add.text(gameWidth - 100, gameHeight - 80, '版本: ' + configer.GAME_VERSION, {
            fontSize: '14px',
            color: '#ffffff'
        });
        // 更新进度条
        this.load.on('progress', (progress: number) => {
            bar.width = 6 + (gameWidth * progress);
        });
        this.load.on('complete', () => {
            loadText.setText('Loading Complete');

            // this.loginCheck();
        })

    }

    loadPlayerSpine(): void {
        this.load.setPath('assets/sanim_224061/')
        this.load.spine('sanim_224061', 'sanim_224061.json', 'sanim_224061.atlas');
        this.load.setPath('assets/sanim_234051/')
        this.load.spine('sanim_234051', 'sanim_234051.json', 'sanim_234051.atlas');

        // 小怪
        this.load.setPath('assets/monster/mh_1017/')
        this.load.spine('mh_1017', 'mh_1017.json', 'mh_1017.atlas');

        this.load.setPath('assets/monster/mh_1310/')
        this.load.spine('mh_1310', 'mh_1310.json', 'mh_1310.atlas');

        this.load.setPath('assets/monster/mh_1303/')
        this.load.spine('mh_1303', 'mh_1303.json', 'mh_1303.atlas');

        this.load.setPath('assets/monster/mh_1304/')
        this.load.spine('mh_1304', 'mh_1304.json', 'mh_1304.atlas');


        // 大怪
        this.load.setPath('assets/monster/mh_5006_2/')
        this.load.spine('mh_5006_2', 'mh_5006_2.json', 'mh_5006_2.atlas');

        this.load.setPath('assets/monster/mh_5006_3/')
        this.load.spine('mh_5006_3', 'mh_5006_3.json', 'mh_5006_3.atlas');

        this.load.setPath('assets/monster/mh_5011/')
        this.load.spine('mh_5011', 'mh_5011.json', 'mh_5011.atlas');

        this.load.setPath('assets/monster/mh_5014/')
        this.load.spine('mh_5014', 'mh_5014.json', 'mh_5014.atlas');

        this.load.setPath('assets/monster/mh_5056/')
        this.load.spine('mh_5056', 'mh_5056.json', 'mh_5056.atlas');
        this.load.setPath('/')

    }

    loadSkill(): void {
        this.load.atlas('huoqiu', 'assets/skill/huoqiu/huoqiu.png', 'assets/skill/huoqiu/huoqiu.json');
        this.load.spine('huonv_luoyan', 'assets/skill/luoyan/seffect_24406110111.json', 'assets/skill/luoyan/seffect_24406110111.atlas')
    }

    loadmap(): void {
        this.load.image('once_tian', 'assets/map/one/tian.png');

        this.load.image('mapmain', 'assets/map/one/mapmain.png');
        this.load.image('cao', 'assets/map/one/cao.png');
        this.load.image('one_grass', 'assets/map/one/grass.png');
        this.load.image('one_tree2', 'assets/map/one/tree2.png');

        this.load.image('once_enclosure1', 'assets/map/one/enclosure1.png');
        this.load.image('once_enclosure2', 'assets/map/one/enclosure2.png');
        this.load.image('once_enclosure3', 'assets/map/one/enclosure3.png');
        this.load.image('once_enclosure4', 'assets/map/one/enclosure4.png');


        this.load.image('one_lousycar', 'assets/map/one/lousycar.png');
        this.load.image('one_ironfence', 'assets/map/one/Ironfence.png');
    }


    loadTa(){
        // this.load.spine('ta04', 'assets/map/ta/01/archer04.json', 'assets/map/ta/01/archer04.atlas')
        this.load.image('cannon02','assets/cannon02.png');
    }

    preload() {
        this.load.image('backgound2', 'assets/bg2.jpg');
        this.load.image('backgound3', 'assets/bg3.jpg');

        this.loadPlayerSpine();
        this.loadSkill();
        this.loadmap();
        this.loadTa();
    }

    create() {
        /** 资源全部加载完成, 正式进入游戏第一个场景 */
        // this.scene.start('MapTest')
        // return

        if (window.client.stateConnect) {
            this.scene.launch('Login');
        }

    }
}
